package com.xuxiangyang.competitionsystem.controller;

import com.xuxiangyang.competitionsystem.entities.*;
import com.xuxiangyang.competitionsystem.service.IUserService;
import com.xuxiangyang.competitionsystem.tools.UUIDTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private UUIDTools tools;

    //@CrossOrigin(origins = "*",maxAge = 3600)
    //验证登录
    @RequestMapping("/checkLoginByTel")
    public RetResult checkLogin(User user) {
        User loginUser;
        loginUser = userService.checkLoginByTel(user);
        if (loginUser == null) {
            return RetResponse.makeErrRsp("手机号或密码输入错误");
        }
        return RetResponse.makeOKRsp(loginUser);
    }

    //查看用户当前密码
    @RequestMapping("/checkOldPass")
    public RetResult checkOldPass(User user) {
        User resultUser = userService.checkOldPass(user);
        System.out.println(user.toString());
        System.out.println(resultUser);
        if(resultUser == null) {
            return RetResponse.makeErrRsp();
        } else {
            return RetResponse.makeOKRsp();
        }
    }

    //修改密码
    @RequestMapping("/updatePass")
    public RetResult updatePass(User user) {
        userService.updatePass(user);
        return RetResponse.makeOKRsp();
    }

    //更新用户信息
    @RequestMapping("/updateUser")
    public RetResult updateUser(User user) {
        System.out.println(user);
        userService.updateUser(user);
        List<User> allUser = userService.getAllUser();
        return RetResponse.makeOKRsp(allUser);
    }

    //获取所有学校
    @RequestMapping("/getAllScholl")
    public RetResult getAllScholl() {
        List<School> schoolList = userService.getAllScholl();
        String[] schools = new String[schoolList.size()];
        for (int i = 0; i < schoolList.size(); i++) {
            schools[i] = schoolList.get(i).getSchoolName();
        }
        return RetResponse.makeOKRsp(schoolList);
    }

    //注册用户
    @RequestMapping("/registerUser")
    public RetResult registerUser(User user) {
        String userId = tools.getUUID();
        user.setUserId(userId);
        System.out.println(user);
        userService.insertUser(user);
        return RetResponse.makeOKRsp();
    }

    //获取所有用户
    @RequestMapping("/getAllUser")
    public RetResult getAllUser() {
        List<User> allUser = userService.getAllUser();
        return RetResponse.makeOKRsp(allUser);
    }

    //删除用户
    @RequestMapping("/deleteUser")
    public RetResult deleteUser(User user) {
        userService.deleteUser(user);
        List<User> allUser = userService.getAllUser();
        return RetResponse.makeOKRsp(allUser);
    }


}
